# -*- coding: utf-8 -*-

# Created on 08/08/2010

# Copyright (C) 2010  Sergio Rodrigo

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import pygame

from core.ActorGraphic import ActorGraphic

BLACK_COLOUR = 0, 0, 0

class Scene:
    """ Scene of the game. Creates the main window (screen) and draws the
    elements.
    
    * Methods:
    __init__(self, width, height)
    addElement(self, element)
    initialize(self)
    update(self)
    draw(self)
    removeElements(self)
    
    * Member variables:
    size -- (width, height) tuple
    screen -- Surface object got by pygame.display.set_mode function
    elements -- List of elements. All of them must have an update method.
    
    """
    def __init__(self, width, height):
        """ Constructor.
        
        * Arguments:
        width -- Integer width of the screen.
        height -- Integer height of the screen.
        
        """
        self.size = (self.width, self.height) = (width, height)
        # TODO: Anyadir esto como parametro "_options"
        self.screen = pygame.display.set_mode(self.size, \
                pygame.HWSURFACE | pygame.DOUBLEBUF)
        # Elementos de la escena, de tipo ActorGraphic y ScoreBoard
        self.elements = []
        #self.elements = pygame.sprite.RenderPlain()

    def addElement(self, element):
        """ Adds an element to the list.
        
        * Arguments:
        element -- Element to add. The element must have an update method.
        
        """
        self.elements.append(element)

    def initialize(self):
        """ Initializes the elements of the screen. 
        
        """
        # TODO: This method calls 'screen' attribute from the elements of the
        # screen. This is unsafe. All the elements must extend a class which
        # have a 'screen' attribute, to make sure they has this attribute.
        # Refactoring: SceneElement superclass for all drawable elements.
        for e in self.elements:
            e.screen = self.screen

    def update(self):
        """ Updates the elements of the scene.
        
        """
        for e in self.elements:
            e.update()

    def draw(self):
        """ Updates the elements, clears the screen, draws the elemens and
        flips the buffers.
        
        """
        # Clean the screen
        self.screen.fill(BLACK_COLOUR)
        #pygame.display.update(self.elements.draw(self.screen))
        for e in self.elements:
            e.draw(self.screen)
        # Flip buffers
        pygame.display.flip()

    def removeElements(self):
        """ Removes the elements of the scene.
        
        """
        # actors
        pygame.sprite.empty()
        # scoreboards ???

# eof
